﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Company.Hardware.Config;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using 无人值守地磅称重系统.Interfaces;
using 无人值守地磅称重系统.Models.Config;

namespace 无人值守地磅称重系统.ViewModels
{
    internal class ConfigViewModel : ObservableObject
    {
        public HardwareConfig HardwareConfig { get; set; }
        public SystemConfig SystemConfig { get; set; }


        public ICommand SaveCommand { get; }
        public ICommand UpdateTemplateCommand{get;}



        public ISession Session { get; }

        public IConfigService ConfigService { get;}

        public ConfigViewModel(ISession session, IConfigService configService)
        {
            Session = session;
            ConfigService = configService;
            HardwareConfig = ConfigService.HardwareConfig;
            SystemConfig = ConfigService.SystemConfig;

            SaveCommand = new RelayCommand(Save);
            UpdateTemplateCommand = new RelayCommand(UpdateTemplate);
        }

        private void UpdateTemplate()
        {
            System.Windows.Forms.OpenFileDialog dialog = new System.Windows.Forms.OpenFileDialog();
            dialog.Filter = "frx files(.frx)|*.frx";
            dialog.FilterIndex= 2;
            dialog.RestoreDirectory= true;
            if(dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                SystemConfig.Template = dialog.FileName;
            }
        }

        private void Save()
        {
            ConfigService.ConfigManager.Write(Company.Core.Config.ConfigKey.HardwareConfig,HardwareConfig);
            ConfigService.ConfigManager.Write(Company.Core.Config.ConfigKey.SystemConfig, SystemConfig);
            Session.Show("保存成功");
        }
    }
}
